package com.zhangkunhao.notes.service;

import com.zhangkunhao.notes.dao.AnswerDao;
import com.zhangkunhao.notes.entity.Do.Answer;
import com.zhangkunhao.notes.entity.Do.PermissionBlacklist;
import com.zhangkunhao.notes.entity.bo.LegalWordsDetector;
import com.zhangkunhao.notes.entity.bo.Message;
import com.zhangkunhao.notes.service.impl.AnswerService;
import com.zhangkunhao.notes.utils.IfValidInputUtil;
import com.zhangkunhao.notes.utils.IllegalWordsDetectorUtils;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 回答接口的实现类
 *
 * @author 20642
 */
public class AnswerServiceImpl implements AnswerService {

    @Override
    public List<Answer> selectAll() {
        return AnswerDao.getInstance().findAll();
    }

    @Override
    public void insert(Answer answer) throws Exception {
        AnswerDao.getInstance().insert(answer);
    }

    @Override
    public int delete(int id) throws SQLException {
        return AnswerDao.getInstance().delete(id);
    }

    @Override
    public int update(Answer answer, String... params) throws Exception {
        return AnswerDao.getInstance().update(answer);
    }

    /**
     * 回答问题
     *
     * @param answer：
     * @return Message<Answer>
     * @throws Exception：
     */
    public Message<Answer> answerQuestion(Answer answer) throws Exception {
        System.out.println("回答对象 " + answer);
        //初始化
        Message<Answer> answerQuestionMessage = new Message<>(null, "你已经被该用户拉黑，回答问题失败", -1);

        //判断
        if (answer == null) {
            answerQuestionMessage.setStatus(0);
            answerQuestionMessage.setResult("当前回答为空");
            return answerQuestionMessage;
        }

        //调用dao校验是否是已经拉黑的用户
        boolean anyMatch = new BlackListServiceImpl().selectAll().stream()
                .anyMatch(blackList -> blackList.getFromUserId().equals(answer.getQuestionUserId())
                        && blackList.getForUserId().equals(answer.getAnswerUserId()));
        //已经拉黑
        if (anyMatch) {
            System.out.println("你已经被该用户拉黑，回答问题失败");
            return answerQuestionMessage;
        }

        //判断校验
        if (!(IfValidInputUtil.ifValidValue(answer.getContent())) || !(IfValidInputUtil.emptyCheckPass(answer.getContent()))) {
            answerQuestionMessage.setStatus(0);
            answerQuestionMessage.setResult("当前回答内容为空");
            return answerQuestionMessage;
        }
        if (!(IfValidInputUtil.ifValidValue(answer.getAnswerUserName()))) {
            answerQuestionMessage.setStatus(0);
            answerQuestionMessage.setResult("当前回答的用户的用户名为空");
            return answerQuestionMessage;
        }

        //调用工具类进行敏感词汇，如诋毁党、⾊情等信息检测
        LegalWordsDetector contentCheck = IllegalWordsDetectorUtils.isLegalWords(answer.getContent());
        if (!contentCheck.isIsLegal()) {
            answerQuestionMessage.setStatus(0);
            answerQuestionMessage.setResult("问题内容" + contentCheck.getMsg());
            return answerQuestionMessage;
        }

        //调用dao
        this.insert(answer);
        List<Answer> answerList = this.selectAll();

        for (Answer answer2 : answerList) {
            if (answer2.getQuestionId().equals(answer.getQuestionId())
                    && answer2.getContent().equals(answer.getContent())
                    && answer2.getAnswerUserName().equals(answer.getAnswerUserName())
                    && answer2.getQuestionUserId().equals(answer.getQuestionUserId())) {
                System.out.println("answerQuestionSuccess");
                answerQuestionMessage.setResult("回答问题成功,已刷新当前页面");
                answerQuestionMessage.setStatus(1);
                answerQuestionMessage.setT(answer2);
                break;
            }
        }
        //返回数据
        return answerQuestionMessage;
    }
}
